﻿/*
 * Created by SharpDevelop.
 * User: Heikki
 * Date: 22.11.2007
 * Time: 20:29
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Data.SQLite;

namespace notebooklet
{
	public class Todo : IDataObj
	{
		private SQLiteConnection db;
		
		private int    id;
		private int    contactId;
		private string summary;
		private string notes;
		
		//private bool isModified; // Onko attribuutteihin koskettu lataamisen jälkeen?
		
		public int     Id {
			get { return id; }
			set { id = value; }
		}
		public int     ContactId {
			get { return contactId; }
			set { contactId = value; }
		}
		public string  Summary {
			get { return summary; }
			set { summary = value; }
		}
		public string  Notes {
			get { return notes; }
			set { notes = value; }
		}
		
		public Todo(SQLiteConnection db) {
			this.db = db;
		}
		
		public Todo(SQLiteConnection db,
					int id,
					int contactId,
		            string summary,
		            string notes) {
			this.db = db;
			this.Id = id;
			this.ContactId = contactId;
			this.Summary = summary;
			this.Notes = notes;
		}
		
		public bool load() { return true; }
		
		public bool load(object[] values) {
			this.Id = System.Convert.ToInt32(values[0].ToString());
			this.ContactId = System.Convert.ToInt32(values[1].ToString());
			this.Summary = values[2].ToString();
			// TODO: Tähän expDate ja expTime
			this.Notes = values[5].ToString();
			// TODO: Tähän priority
			
			return true;
		}
		
		public bool save() {
			SQLiteCommand cmd = db.CreateCommand();
			SQLiteDataReader data;
			
			cmd.CommandText = "SELECT COUNT(*) AS count FROM users WHERE id=" + this.Id + ";";
			data = cmd.ExecuteReader();
			data.Read();
			
			if ( 1 == System.Convert.ToInt32(data["count"].ToString()) ) {
				data.Close();
				
				// TODO: http://xkcd.com/327/	
				cmd.CommandText = "UPDATE users SET " +
					              "contactId = \"" + this.ContactId + "\", " +
				                  "summary = \"" + this.Summary + "\", " +
								  // TODO: Tähän expDate ja expTime
								  "notes = \"" + this.Notes + "\", " +
								  // TODO: Tähän priority
								  "WHERE id = \"" + this.Id + "\";";
				cmd.ExecuteNonQuery();
				return true;
			}
			data.Close();
			
			// TODO: http://xkcd.com/327/
			cmd.CommandText = "INSERT INTO users VALUES(" +
							  this.Id + ", " +
				              "\"" + this.ContactId + "\", " +
			                  "\"" + this.Summary + "\", " +
				              "\"\", " +
							  "\"\", " +
							  "\"" + this.Notes + "\", " +
					          "\"\");";

			cmd.ExecuteNonQuery();
			return true;
		}
		
		// TODO: Aluksi copypaste, myöhemmin jaettuna koodina
		public int getNextId() {
			SQLiteCommand cmd = db.CreateCommand();
			SQLiteDataReader data;
			
			cmd.CommandText = "SELECT id FROM todosSeq";
			data = cmd.ExecuteReader();
			data.Read();
			
			int nextId = System.Convert.ToInt32(data["id"].ToString());
			
			cmd.CommandText = "UPDATE todosSeq SET id = id + 1";
			cmd.ExecuteNonQuery();
			
			return nextId;
		}
	}
}
